package com.sea.supply.service.impl;


import com.codecrane.core.web.BatchImportUtils;
import com.sea.data.service.OrderDetailService;
import com.sea.product.entity.Channel;
import com.sea.product.entity.Product;
import com.sea.supply.entity.*;
import com.sea.supply.mapper.ProduceBalanceMapper;
import com.sea.supply.mapper.ProduceMapper;
import com.sea.supply.service.*;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 陈鸿飞
 */
@Service
public class ProduceBalanceServiceImpl implements ProduceBalanceService {

    @Autowired
    private ProduceBalanceMapper produceBalanceMapper;
    @Autowired
    private ProduceDetailService produceDetailService;

    @Override
    public int save(ProduceBalance produceBalance) {
        return produceBalanceMapper.insert(produceBalance);
    }

    @Override
    public List<ProduceBalance> findByCnd(ProduceBalance produceBalance) {
        return produceBalanceMapper.findByCnd(produceBalance);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importSku(InputStream inputStream, String fileName, String userRealName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<ProduceBalance> list = BatchImportUtils.iteratorForProduce(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少保存一条SKU信息！");
            } else {
                for (ProduceBalance produceBalance : list) {
                    ProduceDetail produceDetail = produceDetailService.getIdByCnd(produceBalance.getOrderSku(), produceBalance.getOrderChannelName(), produceBalance.getOrderDate(), produceBalance.getProduceNo());
                    if(null==produceDetail){
                        throw new RuntimeException("暂未查询到数据!【SKU:"+produceBalance.getOrderSku()+"】");
                    }else {
                        produceBalance.setProduceId(produceDetail.getProduceId());
                        produceBalance.setProduceDetailId(produceDetail.getId());
                        produceBalance.setProduceBeforeNumber(produceDetail.getProduceSurplus());
                        produceBalance.setProduceMember(userRealName);
                        save(produceBalance);



                        if(produceBalance.getProduceAfterNumber()>(produceDetail.getProduceSurplus()-produceDetail.getProduceUse())){
                            throw new RuntimeException("调整数量不能超过剩余数量!【SKU:"+produceBalance.getOrderSku()+"】");

                        }else if((produceDetail.getProduceSurplus()-produceDetail.getProduceShort()-produceBalance.getProduceAfterNumber()-produceDetail.getProduceUse())<0){
                            throw new RuntimeException("调整后数量不能小于0!【SKU:"+produceBalance.getOrderSku()+"】");
                        }else {
                            produceDetail.setProduceShort(produceBalance.getProduceAfterNumber());
                            produceDetailService.updateShort(produceDetail);
                        }
                    }
                }
            }
        }
    }
}
